//
//  DialogXieshang.swift
//  jiaoyi
//
//  Created by wjk on 2018/11/5.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit
import SwiftyJSON

class DialogXieshang: UIView, UITextFieldDelegate{

    
    var cb:cancleBlock?
    var ob:okBlock?
    
    @IBOutlet weak var btnCancle: UIButton!
    
    @IBOutlet weak var btnPlus: UIButton!
    @IBOutlet weak var tfInput: UITextField!
    @IBOutlet weak var labelLow: UILabel!
    @IBOutlet weak var labelLevel: UILabel!
    @IBOutlet weak var labelNo: UILabel!
    @IBOutlet weak var labelCate: UILabel!
    @IBOutlet weak var labelNum: UILabel!
    
    var tg:JSON?
    var da:JSON?
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    static public func showView(frame: CGRect, data:JSON, tagget:JSON) -> DialogXieshang {
        let v = Bundle.main.loadNibNamed("DialogXieshang", owner: nil, options: nil)?.first as! DialogXieshang
        v.frame = frame
        v.tfInput.delegate = v
        print(data.description)
        print(tagget.description)
        v.labelNo.text = data["alias"].stringValue
        v.labelCate.text = data["variety_name"].stringValue
        v.labelLevel.text = data["grade_text"].stringValue
        v.labelNum.text = "\(data["num"].stringValue) \(data["num_unit_text"].stringValue)"
        v.labelLow.text = "\(data["base_price_yuan"].stringValue) 元"
        v.da = data
        v.tg = tagget
        
        if tagget["new_price_show"].stringValue.isEmpty {
        
            v.tfInput.text = String.init(format: "%.2f", data["base_price_show"].floatValue)
        }else {
            v.tfInput.text = String.init(format: "%.2f", tagget["offer_step"].floatValue  + tagget["new_price_show"].floatValue)
        }
        
//        if (tagget["direction"].intValue == 2) {//2、买；1、卖
//            v.btnCancle.isEnabled = false
//        }else {
//            v.btnPlus.isEnabled = false
//        }
        
        return v
    }
    
    init(frame: CGRect, data:JSON) {
        super.init(frame: frame)
        //        return
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //        fatalError("init(coder:) has not been implemented")
    }
    
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
    }
    
    
    @IBAction func jian(_ sender: UIButton) {
        
        
        
        let f = Float(String(self.tfInput.text ?? "0"))! - tg!["offer_step"].floatValue
        
        if f <= 0 {
            showMessage(message: "金额必须大于0")
            return
        }
        
        self.tfInput.text = String.init(format: "%.2f", f)
        
//        if self.tg!["direction"].intValue == 2 {
//            if f <= (tg!["new_price_show"].floatValue + self.tg!["new_price_show"].floatValue) {
//                sender.isEnabled = false
//                return
//            }
//        }
//        self.btnPlus.isEnabled = true
        
    }
    @IBAction func plus(_ sender: UIButton) {
        self.btnCancle.isEnabled = true
        if tg!["offer_step"].floatValue <= 0 {
            return
        }
        
        
        
        
        let f = Float(tfInput.text!)! + tg!["offer_step"].floatValue
        self.tfInput.text = String.init(format: "%.2f", f)
//        self.tfInput.text = "\(f)"
    }
    
    
    @IBAction func ok(_ sender: UIButton) {
        if self.tfInput.text!.isEmpty {
            showMessage(message: "请先输入价格")
            return
        }
        ob?(["price":String(self.tfInput.text?.split(separator: " ").first ?? "0"),"last_price":da!["base_price_show"].stringValue])
    }
    
    @IBAction func cancle(_ sender: UIButton) {
        cb?(["":""])
        self.removeFromSuperview()
    }
}
